package tablet.module_user.activity.teachereslist;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.json_deal.lib_mgson.MGson;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.http.call_back.HttpDialogCallBack;
import com.lib.ui.activity.BaseAppActivity;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.teachereslist.TeacherResListBean;

@Route(path = ARouterPathUtils.Tablet_User_TeacherResListActivity)
public class TeacherResListActivity extends BaseAppActivity {

    @BindView(R2.id.rv_video)
    RecyclerView rvVideo;

    @Autowired(name = "resId")
    int resId;

    HomeAdapter homeAdapter;
    List<TeacherResListBean.ObjBean> list = new ArrayList<>();

    @Override
    protected void onCreateView() {

        resId = getIntent().getIntExtra("recordId", -1);

        initView();

        initRecordVideoData();

    }

    private void initRecordVideoData() {

        if (resId > -1) {

            HttpUtils.with(this)
                    .addParam("requestType", "TEACHING_ASSISTANT_CLASSIFICATION_DETAILS")
                    .addParam("token", SharedPreferenceManager.getInstance(this).getUserCache().getUserToken())
                    .addParam("id", resId)
                    .execute(new HttpDialogCallBack<TeacherResListBean>() {
                        @Override
                        public void onSuccess(TeacherResListBean result) {

                            if (CodeUtil.CODE_200 == result.getCode()) {
                                if (result.getObj() != null) {
                                    list.addAll(result.getObj());
                                    homeAdapter.notifyDataSetChanged();
                                }

                            } else {

                            }
                        }

                        @Override
                        public void onError(String e) {

                        }
                    });
        }
    }

    private void initView() {


        homeAdapter = new HomeAdapter(this, list);
        GridLayoutManager manager = new GridLayoutManager(this, 6);
        rvVideo.setLayoutManager(manager);
        rvVideo.setAdapter(homeAdapter);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_teacher_res_list;
    }


    private class HomeAdapter extends BaseAdapter<TeacherResListBean.ObjBean> {

        public HomeAdapter(Context context, List<TeacherResListBean.ObjBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_userecord;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<TeacherResListBean.ObjBean> mData) {

            final TeacherResListBean.ObjBean rowsBean = mData.get(position);

            ImageView ivBg = holder.getView(R.id.iv_record);
            if (StringUtil.isNotEmpty(rowsBean.getCover_url(), true)) {

                Glide.with(TeacherResListActivity.this)
                        .load(rowsBean.getCover_url())
                        .apply(GlideConfig.getRoundOptions(10))
                        .into(ivBg);
            }
            if (StringUtil.isNotEmpty(rowsBean.getSmall_title(), true)) {
                holder.setText(R.id.tv_record, rowsBean.getSmall_title());
            }

            LinearLayout linItem = holder.getView(R.id.lin_item);
            linItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String resJson = MGson.newGson().toJson(rowsBean);
                    LogUtil.e("教辅资料的数据为：" + resJson);

                    ARouter.getInstance().build(ARouterPathUtils.Tablet_User_TeacherResActivity)
                            .withString("resJson", resJson)
                            .withInt("resId", rowsBean.getId()).navigation();
                }
            });
        }
    }
}
